/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 *
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */


#include <vector>
#include <cstdint>
#include <cmath>
#include <random>
#include <iostream>
#include <algorithm>
#include "acl/acl.h"

#include "../examples/add_with_broadcast/add_with_broadcast.h"

#define CHECK_ACL(x)                                                                        \
    do {                                                                                    \
        aclError __ret = x;                                                                 \
        if (__ret != ACL_ERROR_NONE) {                                                      \
            std::cerr << __FILE__ << ":" << __LINE__ << " aclError:" << __ret << std::endl; \
        }                                                                                   \
    } while (0)

namespace {
static constexpr float REL_TOL = 1e-3f;
static constexpr float ABS_TOL = 1e-5f;

// 判断两个浮点数是否足够接近
bool IsClose(float a, float b) {
    const float eps = 1e-40f; // 防止分母为零
    float diff = std::abs(a - b);
    return (diff <= ABS_TOL) || (diff <= REL_TOL * std::max(std::abs(a), std::abs(b) + eps));
}
}

// BroadcastTo算子的描述：一个输入，一个输出，类型均为float
using BroadcastOpTraitsInt = ATVC::OpTraits<ATVC::OpInputs<int, int>, ATVC::OpOutputs<int>, ATVC::OpTemps<int>>;
using BroadcastOpTraitsFloat = ATVC::OpTraits<ATVC::OpInputs<float, float>, ATVC::OpOutputs<float>, ATVC::OpTemps<float>>;
// Add算子的描述：一个临时输入(Broadcast的输出)，一个输入，一个输出，类型均为float

void AddWithBroadcastCustomInt0(uint32_t blockDim, void* stream, uint8_t* x, uint8_t* y, uint8_t* z, ATVC::BroadcastParam param) {
    AddWithBroadcastCustom<BroadcastOpTraitsInt, ATVC::BROADCAST_POLICY0><<<blockDim, nullptr, stream>>>(x, y, z, param);
}

void AddWithBroadcastCustomInt1(uint32_t blockDim, void* stream, uint8_t* x, uint8_t* y, uint8_t* z, ATVC::BroadcastParam param) {
    AddWithBroadcastCustom<BroadcastOpTraitsInt, ATVC::BROADCAST_POLICY1><<<blockDim, nullptr, stream>>>(x, y, z, param);
}

void AddWithBroadcastCustomFloat0(uint32_t blockDim, void* stream, uint8_t* x, uint8_t* y, uint8_t* z, ATVC::BroadcastParam param) {
    AddWithBroadcastCustom<BroadcastOpTraitsFloat, ATVC::BROADCAST_POLICY0><<<blockDim, nullptr, stream>>>(x, y, z, param);
}

void AddWithBroadcastCustomFloat1(uint32_t blockDim, void* stream, uint8_t* x, uint8_t* y, uint8_t* z, ATVC::BroadcastParam param) {
    AddWithBroadcastCustom<BroadcastOpTraitsFloat, ATVC::BROADCAST_POLICY1><<<blockDim, nullptr, stream>>>(x, y, z, param);
}
